package com.jxsa.point;

import com.alibaba.fastjson.JSON;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author pwq
 */
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException {
        response.addHeader("Content-Type","application/json;charset=UTF-8");
        response.setStatus(200);
        PrintWriter writer = response.getWriter();
        Map<String,Object> map = new HashMap<>(3);
        map.put("msg","无访问权限");
        map.put("data",e.getMessage());
        map.put("code",400);
        writer.print(JSON.toJSONString(map));
        writer.flush();
        writer.close();
    }
}
